//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pt;

import com.aidong.dao.wills.CardTypeCodeDao;
import com.aidong.dao.wills.ContractSignDao;
import com.aidong.dao.wills.DicShopAreaCodeDao;
import com.aidong.dao.wills.MemberContractDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.MemberUseDao;
import com.aidong.dao.wills.PtBespeakDao;
import com.aidong.dao.wills.PtCoachCodeDao;
import com.aidong.dao.wills.PtContractDao;
import com.aidong.model.wills.CardTypeCode;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PtBespeak;
import com.aidong.model.wills.PtCoachCode;
import com.aidong.model.wills.PtContract;
import com.aidong.service.SmsListService;
import com.aidong.web.controller.BaseController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"ptBespeak"})
public class PtBespeakController extends BaseController {
    @Resource
    PtBespeakDao ptBespeakDao;
    @Resource
    PtContractDao ptContractDao;
    @Resource
    PtCoachCodeDao ptCoachCodeDao;
    @Resource
    MemberInfoDao memberInfoDao;
    @Resource
    SmsListService smsListService;
    @Resource
    MemberContractDao memberContractDao;
    @Resource
    CardTypeCodeDao cardTypeCodeDao;
    @Resource
    DicShopAreaCodeDao dicShopAreaCodeDao;
    @Resource
    MemberUseDao memberUseDao;
    @Resource
    ContractSignDao contractSignDao;

    public PtBespeakController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("pt/bespeak/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "date",required = false) String date, @RequestParam(value = "cardCode",required = false) String cardCode, @RequestParam(value = "coachCode",required = false) String coachCode, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt1 = this.parseDate(date, sdf);
        if(dt1 == null) {
            dt1 = this.parseDate(sdf.format(new Date()), sdf);
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(dt1);
        cal.add(5, 1);
        Date dt2 = cal.getTime();
        MemberInfo member = null;
        Object coachList = new ArrayList();
        if(!this.chkNull(cardCode)) {
            member = this.memberInfoDao.getByCardCode(cardCode);
            if(member != null) {
                coachList = this.ptCoachCodeDao.getListByMemberShop(member.getMemberCode(), this.getLoginShopCode());
            }
        } else {
            coachList = this.ptCoachCodeDao.getShopCoachList(this.getLoginShopCode());
        }

        LinkedHashMap ptmap = new LinkedHashMap();

        for(int i = 0; i < ((List)coachList).size(); ++i) {
            String mv = ((PtCoachCode)((List)coachList).get(i)).getCode();
            List ls;
            if(member != null) {
                ls = this.ptBespeakDao.getListByCoachDay(mv, member.getMemberCode(), dt1, dt2);
            } else {
                ls = this.ptBespeakDao.getListByCoachDay(mv, dt1, dt2);
            }

            if(ls != null && ls.size() > 0) {
                Iterator i$ = ls.iterator();

                while(i$.hasNext()) {
                    PtBespeak p = (PtBespeak)i$.next();
                    if(p.getClassHour().intValue() < 8) {
                        p.setClassHour(Integer.valueOf(8));
                    }

                    if(p.getClassHour().intValue() > 22) {
                        p.setClassHour(Integer.valueOf(22));
                    }
                }

                ptmap.put(mv, ls);
            }
        }

        ModelAndView var18 = new ModelAndView("pt/bespeak/_list");
        var18.addObject("coachList", coachList);
        var18.addObject("ptmap", ptmap);
        if(member != null) {
            var18.addObject("cardCode", member.getCardCode());
        }

        var18.addObject("dt", dt1);
        var18.addObject("dt0", sdf.format(dt1));
        var18.addObject("dtn", sdf.format(cal.getTime()));
        cal.add(5, -2);
        var18.addObject("dtp", sdf.format(cal.getTime()));
        return var18;
    }

    @RequestMapping({"add"})
    public ModelAndView add(@RequestParam(value = "date",required = false) String date, @RequestParam(value = "cardCode",required = false) String cardCode, @RequestParam(value = "hour",required = false) Integer hour) {
        ModelAndView mv = new ModelAndView("pt/bespeak/add");
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date0 = sdf.format(cal.getTime());
        if(this.chkNull(date) || date.compareTo(date0) < 0) {
            date = date0;
        }

        int hh = cal.get(11);
        int mm = cal.get(12);
        if(!this.chkNull(hour) && hour.intValue() >= 8 && hour.intValue() <= 22) {
            hh = hour.intValue();
            mm = 0;
        }

        if(!this.chkNull(cardCode)) {
            mv.addObject("cardCode", cardCode);
        }

        mv.addObject("date0", date0);
        mv.addObject("date", date);
        mv.addObject("hh", Integer.valueOf(hh));
        mv.addObject("mm", Integer.valueOf(mm));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@RequestParam("contractCode") String contractCode, @RequestParam("date") String date, @RequestParam("hh") String hh, @RequestParam("mm") String mm) {
        if(this.chkNull(contractCode)) {
            return this.getResult(false, "请选择合同号");
        } else if(!this.chkNull(date) && !this.chkNull(hh) && !this.chkNull(mm)) {
            PtContract contract = this.ptContractDao.getByContractCode(contractCode);
            if(contract != null && contract.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                if(contract.getBespeakQuantity().intValue() >= contract.getQuantity().intValue()) {
                    return this.getResult(false, "合同已经没有课时");
                } else {
                    Integer bespQuan = this.ptBespeakDao.getBespeakCount(contractCode);
                    if(!this.chkNullZ(bespQuan) && bespQuan.intValue() >= contract.getQuantity().intValue()) {
                        return this.getResult(false, "合同已经没有课时");
                    } else {
                        if(hh.length() == 1) {
                            hh = "0" + hh;
                        }

                        if(mm.length() == 1) {
                            mm = "0" + mm;
                        }

                        Date classTime = this.parseDate(date + " " + hh + ":" + mm + ":00", "yyyy-MM-dd HH:mm:ss");
                        if(System.currentTimeMillis() - classTime.getTime() > 3600000L) {
                            return this.getResult(false, "不能预约以过的时间");
                        } else if(!this.chkCanEnter(contract.getMemberCode(), classTime)) {
                            return this.getResult(false, "无有效会籍合同,不能预约");
                        } else {
                            Date dt1 = this.parseDate(date + " " + hh + ":00:00", "yyyy-MM-dd HH:mm:ss");
                            Date dt2 = this.parseDate(date + " " + hh + ":59:00", "yyyy-MM-dd HH:mm:ss");
                            List ls = this.ptBespeakDao.getListByCoachDay(contract.getCoachCode(), dt1, dt2);
                            if(ls.size() > 0) {
                                return this.getResult(false, "教练该时段已有预约");
                            } else {
                                PtBespeak bespeak = new PtBespeak();
                                bespeak.setShopCode(this.getLoginShopCode());
                                bespeak.setContractCode(contract.getContractCode());
                                bespeak.setMemberCode(contract.getMemberCode());
                                bespeak.setBespeakDate(new Date());
                                bespeak.setBespeakType("1");
                                bespeak.setPtCode(contract.getPtCode());
                                bespeak.setCoachCode(contract.getCoachCode());
                                bespeak.setClassTime(classTime);
                                bespeak.setStatus(Integer.valueOf(2));
                                bespeak.setPrintLabel(Integer.valueOf(0));
                                bespeak.setCreateUser(this.getLoginUserCode());
                                bespeak.setCreateTime(new Date());
                                bespeak.setUpdateTime(new Date());

                                try {
                                    this.ptBespeakDao.addBespeak(bespeak);
                                    this.smsListService.addSmsList("22", bespeak.getAutoId().intValue());
                                    this.addUserLog("私教预约", "流水号:" + bespeak.getAutoId() + ",合同号:" + bespeak.getContractCode() + ",会员:" + bespeak.getMemberCode());
                                    if(bespeak.getClassTime().getTime() < this.getTrimDate(new Date()).getTime() + 86400000L && this.memberUseDao.getTodayInByMemberCode(bespeak.getShopCode(), bespeak.getMemberCode()) != null) {
                                        this.ptBespeakDao.addBespeakSign(bespeak);
                                    }
                                } catch (Exception var13) {
                                    this.log.error("add ptbespeak", var13);
                                    return this.getResult(false, "预约发生错误");
                                }

                                return this.getResult(true, "预约成功");
                            }
                        }
                    }
                }
            } else {
                return this.getResult(false, "合同号错误");
            }
        } else {
            return this.getResult(false, "请输入预约时间");
        }
    }

    private boolean chkCanEnter(String memberCode, Date date) {
        List list = this.memberContractDao.getEnterListByMemberCode(memberCode, date);
        if(list != null && list.size() != 0) {
            String shopCode = this.getLoginShopCode();
            Iterator i$ = list.iterator();

            while(true) {
                MemberContract contract;
                do {
                    if(!i$.hasNext()) {
                        return false;
                    }

                    contract = (MemberContract)i$.next();
                } while(contract.getTotalCount().intValue() > 0 && contract.getRestCount().intValue() == 0);

                if(this.memberContractDao.chkContractShop(contract.getContractCode(), shopCode)) {
                    return true;
                }

                if(!this.chkNull(contract.getManyShop())) {
                    CardTypeCode cardType = (CardTypeCode)this.cardTypeCodeDao.getById(contract.getCardType());
                    if(cardType != null && cardType.getManyShopQuantity().intValue() == 0 && this.dicShopAreaCodeDao.chkAreaShop(contract.getManyShop(), shopCode)) {
                        return true;
                    }
                } else if(shopCode.equalsIgnoreCase(contract.getShopCode())) {
                    return true;
                }
            }
        } else {
            return false;
        }
    }

    @RequestMapping({"getMemberContract"})
    @ResponseBody
    public String getMemberContract(@RequestParam("cardCode") String cardCode) {
        MemberInfo member = this.memberInfoDao.getByCardCode(cardCode);
        if(member == null) {
            return this.getResult(false, "会员卡号错误");
        } else {
            List list = this.ptContractDao.getValidListByMemberCode(member.getMemberCode(), this.getLoginShopCode());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            StringBuilder sb = new StringBuilder();
            sb.append("{cardCode:\"").append(member.getCardCode()).append("\"").append(",memberCode:\"").append(member.getMemberCode()).append("\"").append(",memberName:\"").append(member.getChName()).append("\"").append(",contract:[");
            int n = 0;

            PtContract contract;
            for(Iterator i$ = list.iterator(); i$.hasNext(); sb.append("{contractCode:\"").append(contract.getContractCode()).append("\"").append(",beginTime:\"").append(sdf.format(contract.getBeginTime())).append("\"").append(",endTime:\"").append(sdf.format(contract.getEndTime())).append("\"").append(",ptCode:\"").append(contract.getPtCode()).append("\"").append(",ptCodeName:\"").append(this.cache.getValue("ptItemName", contract.getPtCode())).append("\"").append(",coachCode:\"").append(contract.getCoachCode()).append("\"").append(",coachName:\"").append(this.cache.getValue("ptCoachName", contract.getCoachCode())).append("\"").append(",quantity:").append(contract.getQuantity()).append("").append(",leftQuantity:").append(contract.getQuantity().intValue() - contract.getBespeakQuantity().intValue()).append("").append("}")) {
                contract = (PtContract)i$.next();
                if(n++ > 0) {
                    sb.append(",");
                }
            }

            sb.append("]}");
            return this.getSuccess(sb.toString(), "");
        }
    }

    @RequestMapping({"view"})
    public ModelAndView view(@RequestParam("id") int id) {
        PtBespeak bespeak = (PtBespeak)this.ptBespeakDao.getById(Integer.valueOf(id));
        if(bespeak == null) {
            return this.showAlert("没有这个预约");
        } else {
            MemberInfo member = this.memberInfoDao.getById(bespeak.getMemberCode());
            if(member == null) {
                return this.showAlert("会员信息错误");
            } else {
                boolean prt = bespeak.getStatus().intValue() != 2?false:this.getAccountDate(new Date()).equals(this.getAccountDate(bespeak.getClassTime()));
                ContractSign sign = this.contractSignDao.getBySaleId(bespeak.getAutoId().intValue(), "10");
                ModelAndView mv = new ModelAndView("pt/bespeak/view");
                mv.addObject("bespeak", bespeak);
                mv.addObject("member", member);
                if(prt) {
                    mv.addObject("prt", "1");
                }

                if(sign != null && sign.getStatus().intValue() != 1) {
                    mv.addObject("sign", "1");
                }

                return mv;
            }
        }
    }

    @RequestMapping({"_cancel"})
    @ResponseBody
    public String _cancel(@RequestParam("id") int id) {
        PtBespeak bespeak = (PtBespeak)this.ptBespeakDao.getById(Integer.valueOf(id));
        if(bespeak != null && bespeak.getStatus().intValue() != 0) {
            if(bespeak.getStatus().intValue() == 1) {
                return this.getResult(false, "已经核销,不能取消");
            } else if(bespeak.getPrintLabel().intValue() == 1) {
                return this.getResult(false, "已经打印小票,不能取消");
            } else if(bespeak.getStatus().intValue() != 2) {
                return this.getResult(false, "不能取消");
            } else {
                ContractSign sign = this.contractSignDao.getBySaleId(bespeak.getAutoId().intValue(), "10");
                if(sign != null && sign.getStatus().intValue() != 1) {
                    return this.getResult(false, "已经签名,不能取消");
                } else {
                    bespeak.setStatus(Integer.valueOf(0));
                    bespeak.setCanceType("1");
                    bespeak.setCancelUser(this.getLoginUserCode());
                    bespeak.setCreateTime(new Date());

                    try {
                        this.ptBespeakDao.updateCancel(bespeak);
                        if(sign != null) {
                            this.contractSignDao.delete(sign);
                        }

                        this.addUserLog("预约取消", "流水号:" + bespeak.getAutoId() + ",合同号:" + bespeak.getContractCode() + ",会员号:" + bespeak.getMemberCode());
                    } catch (Exception var5) {
                        this.log.error("ptbespeak cancel", var5);
                        return this.getResult(false, "取消发生错误");
                    }

                    return this.getResult(true, "取消成功");
                }
            }
        } else {
            return this.getResult(false, "没有这个预约");
        }
    }

    @RequestMapping({"print"})
    public ModelAndView print(@RequestParam("id") int id) {
        PtBespeak bespeak = (PtBespeak)this.ptBespeakDao.getById(Integer.valueOf(id));
        if(bespeak == null) {
            return this.showAlert("没有这个预约");
        } else {
            MemberInfo member = this.memberInfoDao.getById(bespeak.getMemberCode());
            if(member == null) {
                return this.showAlert("会员信息错误");
            } else {
                PtContract contract = this.ptContractDao.getByContractCode(bespeak.getContractCode());
                if(contract == null) {
                    return this.showAlert("私教合同错误");
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    StringBuilder sb = new StringBuilder();
                    sb.append("{saleId:").append(bespeak.getAutoId()).append("").append(",contractCode:\"").append(contract.getContractCode()).append("\"").append(",shopCode:\"").append(contract.getShopCode()).append("\"").append(",shopName:\"").append(this.cache.getValue("shopName", contract.getShopCode())).append("\"").append(",saleDate:\"").append(sdf.format(contract.getSaleDate())).append("\"").append(",beginTime:\"").append(sdf.format(contract.getBeginTime())).append("\"").append(",endTime:\"").append(sdf.format(contract.getEndTime())).append("\"").append(",bespeakTime:\"").append(sdf.format(bespeak.getBespeakDate())).append("\"").append(",classTime:\"").append(sdf.format(bespeak.getClassTime())).append("\"").append(",ptCode:\"").append(bespeak.getPtCode()).append("\"").append(",ptCodeName:\"").append(this.cache.getValue("ptItemName", bespeak.getPtCode())).append("\"").append(",coachCode:\"").append(bespeak.getCoachCode()).append("\"").append(",coachName:\"").append(this.cache.getValue("ptCoachName", bespeak.getCoachCode())).append("\"").append(",quantity:").append(contract.getQuantity()).append("").append(",leftQuantity:").append(contract.getQuantity().intValue() - contract.getBespeakQuantity().intValue()).append("").append(",cardCode:\"").append(member.getCardCode()).append("\"").append(",memberCode:\"").append(member.getMemberCode()).append("\"").append(",memberName:\"").append(member.getChName()).append("\"").append(",gender:\"").append(this.cache.getValue("gender", member.getGender())).append("\"").append(",idType:\"").append(member.getIdType()).append("\"").append(",idNumber:\"").append(member.getIdNumber()).append("\"").append(",mobile:\"").append(member.getMobile()).append("\"").append(",printDate:\"").append(sdf.format(new Date())).append("\"").append("}");
                    ModelAndView mv = new ModelAndView("print/print");
                    mv.addObject("data", sb.toString());
                    mv.addObject("title", "私教核销");
                    mv.addObject("ptjs", "ptbespeak");
                    mv.addObject("_picPath", this.getSetting("pic.path"));
                    return mv;
                }
            }
        }
    }

    @RequestMapping({"_print"})
    @ResponseBody
    public String _print(@RequestParam("id") int id) {
        PtBespeak bespeak = (PtBespeak)this.ptBespeakDao.getById(Integer.valueOf(id));
        if(bespeak != null && bespeak.getStatus().intValue() != 0) {
            if(bespeak.getPrintLabel().intValue() == 1) {
                return this.getResult(false, "已经打印小票");
            } else {
                bespeak.setPrintLabel(Integer.valueOf(1));
                bespeak.setPrintUser(this.getLoginUserCode());
                bespeak.setPrintTime(new Date());

                try {
                    this.ptBespeakDao.updatePrint(bespeak);
                    this.addUserLog("预约核销打印", "流水号:" + bespeak.getAutoId() + ",合同号:" + bespeak.getContractCode() + ",会员号:" + bespeak.getMemberCode());
                } catch (Exception var4) {
                    this.log.error("ptbespeak print", var4);
                    return this.getResult(false, "打印发生错误");
                }

                return this.getResult(true, "打印完成");
            }
        } else {
            return this.getResult(false, "没有这个预约");
        }
    }
}
